﻿using Furion.DependencyInjection;
using LS.Nc6.Application.System;
using LS.Nc6.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections;


namespace LS.Nc6.Web.Entry.Controllers
{
    [Authorize]
    public class AdminController : BaseAdminController
    {
        private readonly ISysMenuService _menuService;
        private  readonly ICache _cache;

        
        public AdminController(ISysMenuService menuService,IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed) 
            :base(cacheOptions,resolveNamed)
        {
            _menuService = menuService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }

        /// <summary>
        /// 管理员首页
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Index()
        {
            string tag = "System";
            if(!string.IsNullOrEmpty(Request.Query["tag"]))
            {
                tag = Request.Query["tag"];
            }
            ViewBag.Tag = tag;

            ViewBag.UserId = GetUserId();

            ViewBag.UserName = GetUserName();

            var sysinfo =await SysInfoModelCache();
            if (sysinfo==null)
            {
                return ErrorTimeOut();
            }
            ViewBag.SysTitle = sysinfo.SysTitle;
            ViewBag.SysLogo = sysinfo.SysLogo;
            ViewBag.MuiltTab = sysinfo.MuiltTab; 
            ViewBag.DefaultColor = sysinfo.DefaultColor;
            ViewBag.SysFooter = sysinfo.SysFooter;
            ViewBag.CopyRight = sysinfo.SysCopyRight;


            return View();
        }
        public async Task<ActionResult> Welcome()
        {
            var sysinfo = await SysInfoModelCache();

            if (sysinfo.SysType ==1)
            {
                return View();
            }
            else
            {
                return View("CompanyWelcome");
            }
        }

        public ActionResult Notice()
        {
            return View();
        }

      
        #region 公有方法
        public dynamic GetMenu()
        {
            return CurentMenu.Where(a => a.isMenu == true); ;
        }
        public dynamic ClearMenuCache()
        {
            _cache.DelAsync("CurentMenu_" + GetUserId());
            _cache.DelAsync("CurentPower_" + GetUserId());
            _cache.DelAsync("LS_Nc6_SysInfo");
            return "操作成功";
        }

        /// <summary>
        /// 获取分类文章数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseDataResult GetChannelNum()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < 6; i++)
            {
                Hashtable hs = new Hashtable ();
                hs.Add("text","分类");
                hs.Add("value", 100 * (i + 1) + new System.Random().Next(1, 99));
                list.Add(hs);
            }
            dr.data = list;
            return dr;
        }

        /// <summary>
        /// 获取分类文章排名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseDataResult GetRankList()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < 15; i++)
            {
                Hashtable hs = new Hashtable();
                hs.Add("no",i+1);
                hs.Add("title", "测试新闻测试新闻测试新闻测试新闻");
                hs.Add("datetime", DateTime.Now.ToString("yy-MM-dd"));
                list.Add(hs);
            }
            dr.data = list;
            return dr;
        }

        [HttpGet]
        public ResponseDataResult GetBrokenLine()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            List<string> xAxis = new List<string>();
            List<int> yAxis = new List<int>();
            for (int i = 0; i < 30; i++)
            {
                xAxis.Add((i + 1)+"日");
                yAxis.Add(100 * (i + 1) + new System.Random().Next(1, 99));
            }
            dr.data = new { xAxis, yAxis };
            return dr;
        }
     

        #endregion

        #region 私有方法
  
        #endregion

    }
}